package com.example.example.service.impl;

import com.example.example.aiService.Assistant;
import com.example.example.service.IChatService;
import com.example.example.store.PersistentChatMemoryStore;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

@Service
public class ChatService implements IChatService {

    @Autowired
    @Qualifier("kimiStreamingChatModel")
    private StreamingChatLanguageModel kimiStreamingChatModel;

    private Assistant kimiMode;

    @Autowired
    private PersistentChatMemoryStore chatMemoryStore;

    @PostConstruct
    public void init() {
        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(chatMemoryStore)
                .build();

        kimiMode = AiServices.builder(Assistant.class)
                .streamingChatLanguageModel(kimiStreamingChatModel)
                .chatMemoryProvider(chatMemoryProvider)
                .build();
    }

    @Override
    public Flux<String> kimiChat(String userMessage) {

        return kimiMode.chat(1, userMessage);

    }

}
